/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.utility;


import com.edu.kamai.config.Constant;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author xzhu
 */
public class FileName {

    public static String getCurrentFolder(){
        File directory = new   File("."); 
        try{
            return directory.getCanonicalPath();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
    
    public static String reNameWithCurrentTime(String rename){
        System.out.println("before:" + rename);
        String ext,fname;
        int pos = rename.lastIndexOf("."); 
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MMMMM_dd_GGG_hh_mm_aaa", Locale.US);
        String data2Str =  simpleDateFormat.format(new Date());
        if(pos != -1){
            ext = rename.substring(pos);
            fname =rename.substring(0, pos);
        }else{
            ext = "";
            fname = rename;
        }
        fname = fname + "_" + data2Str;
        fname = MD5.Md5(fname);
        System.out.println("after:" + fname+ext);
        return fname+ext;
    }
    
    public static String reFullPathWithTime(String fullPath){
        String retFullPath = getUploadFolder(fullPath);
        int pos = fullPath.lastIndexOf(File.separator);
        String filename = fullPath.substring(pos + 1);
        retFullPath = retFullPath + "/" + reNameWithCurrentTime(filename);
        return retFullPath;
    }
    
    public static String reAbsPathWithTime(String fullPath){
        String retAbsPath = "";
        int pos = fullPath.lastIndexOf(File.separator);
        String filename = fullPath.substring(pos + 1);
        retAbsPath = reNameWithCurrentTime(filename);
        return retAbsPath;
    }
    
    
    public static String getUploadFolder(String fullPath){
        String retFolder = "";
        int pos = fullPath.lastIndexOf("."); 
        String ext = fullPath.substring( pos + 1);
        if(ext.equalsIgnoreCase("mp3") || ext.equalsIgnoreCase("wma")){
            retFolder = Constant.upLoadMMediaFileFolder;
        }else if(ext.equalsIgnoreCase("xml")){
            retFolder = Constant.upLoadPaperFileFolder;
        }else if(ext.equalsIgnoreCase("wav")){
            retFolder = Constant.upLoadRECFileFolder;
        }
        //System.out.println("retFolder:" + retFolder);
        return retFolder;
    }
    
    public static String getDefaultKamaiFolder(){
        FileSystemView fsv = new FileSystemView() {
            @Override
            public File createNewFolder(File containingDir) throws IOException {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };
                
        //System.out.println(fsv.getHomeDirectory().toString());        
        String folder = fsv.getHomeDirectory().toString() + File.separator +
                "kamai" + File.separator;
        File file = new File(folder);
        if (!file.exists()){
            file.mkdirs();
        }
        return folder;
    }
    
    public static void main(String[] arg){
        //System.out.println(FileName.reFullPathWithTime("c:\\abc\\edf\\123.mp3"));
        //System.out.println(FileName.reFullPathWithTime("d:\\abc\\edf\\456.xml"));
        //System.out.println(FileName.getCurrentFolder());
        //abc();
    }
}
